package com.itheima.tanhua.autoconfig.template;

import com.baidu.aip.face.AipFace;
import com.itheima.tanhua.autoconfig.properties.FaceProperties;
import org.json.JSONObject;

import java.util.HashMap;

/**
 * @author Lff
 * @date 2022/3/23
 * 人脸识别
 */
public class FaceTemplate {
    private FaceProperties faceProperties;

    public FaceTemplate(FaceProperties faceProperties) {
        this.faceProperties = faceProperties;
    }

    private FaceTemplate() {
    }

    public boolean hasFace(String imageUrl) {
        // 初始化一个AipFace
        AipFace client = new AipFace(faceProperties.getAppId()
                , faceProperties.getApiKey(), faceProperties.getSecretKey());
        // 可选：设置网络连接参数
        client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(60000);
        // 调用接口
        String imageType = faceProperties.getImageType();
        // 传入可选参数调用接口
        HashMap<String, String> options = new HashMap<String, String>();
        options.put("face_field", "age");
        options.put("max_face_num", "2");
        options.put("face_type", "LIVE");
        options.put("liveness_control", "LOW");
        // 人脸检测
        JSONObject res = client.detect(imageUrl, imageType, options);
        Integer error_code = (Integer) res.get("error_code");
        System.out.println(res);
        //返回结果
        return error_code == 0;
    }
}
